package a12_06_jdbc;




import java.sql.*;
import java.util.UUID;

/**
 * description:数据库工具类，连接和操作数据库
 * author: xiaoxu
 * createTime: 2018-12-05 7:03
 *
 * jdbc:Java DataBase Connectivity
 * 做什么？ 用java代码连接数据库，并操作数据库。
 *
 **/
public class DBUtil {

    //连接数据库的url
    private static final String URL =
            "jdbc:mysql://39.105.44.223:3306/ZhangXu?useUnicode=true&amp;characterEncoding=utf-8";
    //登录名
    private static final String USER = "dbadmin";
    //登录密码
    private static final String PASSWORD = "dbadmin";
    //连接类：执行SQL语句，返回结果
    private static Connection conn = null;

    static {
        try {
            //1.加载驱动程序，forName里边的内容是固定的
            //--用到到【反射】的知识点
            //com.mysql.jdbc.Driver 包名+类名
            //com.mysql.jdbc1.Driver
            Class.forName("com.mysql.jdbc.Driver");
            //2.获得数据库连接，连接上数据库
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws Exception {

        Biology biology = new Biology();
        biology.setId("2");
        select(biology);
        //java.lang.NullPointerException
        /*biology.setId(UUID.randomUUID().toString());
        biology.setName("zhu");
        biology.setCurrentLookAfterCount(0);
        biology.setNeedCount(4);
        insert(biology);*/


        System.out.println(UUID.randomUUID().toString());
        System.out.println(UUID.randomUUID().toString());
        System.out.println(UUID.randomUUID().toString());
        System.out.println(UUID.randomUUID().toString());

    }



    /*
        throws SQLException :定义该方法抛出的异常 ，SQLException异常类型
        throws : 是一个规范限制，你用我的方法，
            要么继续抛出（throws）异常，要么捕获（try）异常
            作用：当程序出现异常（错误），你会知道错误出现在什么地方
     */
    public static void select(Biology biology) throws SQLException {
        //不灵活
        //String sql = "select * from biology where id = 1 or needCount = 4";
        //String sql = "select * from biology where id = ? or needCount = ?";
        String sql = "select * from biology where id = ? ";
        //创建PreparedStatement对象，用来向数据库发送带参数的sql语句
        PreparedStatement pstm = conn.prepareStatement(sql);
        //按照顺序设置参数
        pstm.setString(1, biology.getId());
        //id = 2  select * from biology where id = 2
        //pstm.setInt(2, needCount);
        //执行SQL语句，返回查询的结果
        ResultSet rs = pstm.executeQuery();
        //判断是否有下一行数据
        while (rs.next()) {
            System.out.println(rs.getString("name"));
            System.out.println(rs.getInt("needCount"));
        }

    }

    public static void insert(Biology biology) throws SQLException {
        String sql = "insert into biology" +
                "(id,name,needCount,currentLookAfterCount,createTime,updateTime)" +
                "values(" +
                "?,?,?,?,now(),current_date())";
        PreparedStatement ptmt = conn.prepareStatement(sql);
        ptmt.setString(1, biology.getId());
        ptmt.setString(2, biology.getName());
        ptmt.setInt(3, biology.getNeedCount());
        ptmt.setInt(4, biology.getCurrentLookAfterCount());
        boolean success = ptmt.execute();


    }







}
